<?php
include_once(dirname(__FILE__).'/config.php');

$store = ARC2::getStore($arc_config);

$URLPrefix = "http://desanti.org/";

$insertQueryPrefix =  "PREFIX cols: <" . $URLPrefix . "schemas/collections#>\n";
$insertQueryPrefix .= "INSERT INTO <" . $URLPrefix . "> {\n";

$deleteQueryPrefix = "PREFIX cols: <" . $URLPrefix . "schemas/collections#>\n";
$deleteQueryPrefix .= "DELETE {\n";

$root = dirname(__FILE__).'/';

function id($file) {
	global $root, $URLPrefix;
	$id = substr_replace($file, $URLPrefix, 0, strlen($root));
	return $id;
}


$directories = new DirectoryIterator($root.'collections');

$fstLevelDirectoriesNames = array();

foreach( $directories as $dir )
{
	$fstLevelDirectoriesNames[] = $dir->getFilename();
}

sort( $fstLevelDirectoriesNames );

$collectionsToTreat = array( 148, 149, 150, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 168, 170, 171, 172, 173, 200, 201, 202, 203 );

foreach( $fstLevelDirectoriesNames as $directoryName )
{
	$numCollection = intval( $directoryName );

	if( ! array_shift( str_split( $directoryName ) ) == '.' &&
	    $numCollection > 202 &&
	    in_array( $numCollection, $collectionsToTreat ) )
	{
		$dir_iterator = new RecursiveDirectoryIterator($root.'collections/'.$directoryName);
		$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);

		foreach ($iterator as $image) 
		{
			if ($image->isFile() && strpos($image, '_high.jpeg')) 
			{
				$q = $insertQueryPrefix;
				$d = $deleteQueryPrefix;

				$page = substr(id($image), 0, strpos(id($image), '_high.jpeg')); 
				$thumbnail = $page . '_low.jpeg';

				$d .= '<' . id($image) . '> cols:hasWidth ?iw .' . "\n";
				$d .= '<' . id($image) . '> cols:hasHeight ?ih .' . "\n";
				$d .= '<' . $thumbnail . '> cols:hasWidth ?tw .' . "\n";
				$d .= '<' . $thumbnail . '> cols:hasHeight ?th .' . "\n";

				$d .= "}\nwhere{\n";

				$d .= '<' . id($image) . '> cols:hasWidth ?iw .' . "\n";
				$d .= '<' . id($image) . '> cols:hasHeight ?ih .' . "\n";
				$d .= '<' . $thumbnail . '> cols:hasWidth ?tw .' . "\n";
				$d .= '<' . $thumbnail . '> cols:hasHeight ?th .' . "\n";

				$d .= "}\n";

				$imgSize = getimagesize(id($image));
				$thumbSize = getimagesize($thumbnail);

				$q .= '<' . id($image) . '> cols:hasWidth "' . $imgSize[0] . '" .' . "\n";
				$q .= '<' . id($image) . '> cols:hasHeight "' . $imgSize[1] . '" .' . "\n";
				$q .= '<' . $thumbnail . '> cols:hasWidth "' . $thumbSize[0] . '" .' . "\n";
				$q .= '<' . $thumbnail . '> cols:hasHeight "' . $thumbSize[1] . '" .' . "\n";

				$q .= "}\n";

				$store->query($d);
				if ($errors = $store->getErrors())
				{
					print 'requete en cause ';
					print $q;
					print join("\n", $errors);
					exit (10);
				}

				$store->query($q);
				if ($errors = $store->getErrors())
				{
					print 'requete en cause ';
					print $d;
					print join("\n", $errors);
					exit (10);
				}
			}
		}
		
		print $directoryName;
	}
}

?>
